package com.huawei.hms.framework.network.restclient.hwhttp;

import com.huawei.hms.framework.common.StringUtils;
import com.huawei.hms.framework.network.restclient.Headers;
import com.huawei.hms.framework.network.restclient.Version;
import com.huawei.hms.framework.network.restclient.hwhttp.Interceptor.Chain;
import com.huawei.hms.framework.network.util.ContextUtil;
import java.io.IOException;

public class CallServerInterceptor implements Interceptor {
    public Response intercept(Chain chain) throws IOException {
        return ((RealInterceptorChain) chain).getRequestTask().execute(checkHead(chain.request()));
    }

    public Request checkHead(Request request) {
        Object obj = null;
        Headers headers = request.getHeaders();
        for (int i = 0; i < headers.size(); i++) {
            String name = headers.name(i);
            if (name != null && StringUtils.toLowerCase(name).equals("user-agent")) {
                obj = 1;
                break;
            }
        }
        if (obj == null) {
            return request.newBuilder().addHeader("User-Agent", Version.getUserAgent(ContextUtil.getContext())).build();
        }
        return request;
    }
}
